﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using DAO;

namespace BLL
{
    public class UserManager
    {
        /// <summary>
        ///     判断用户的用户名和密码是否正确
        /// </summary>
        /// <param name="username" type="string">
        ///     <para>
        ///         登录名
        ///     </para>
        /// </param>
        /// <param name="password" type="string">
        ///     <para>
        ///         密码
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回true , 则正确
        /// </returns>
        public bool UserLogin(String username, String password)
        {
            DataTable DT_UserInfo = Globle.userService.GetUserByName(username);
            if (DT_UserInfo != null && DT_UserInfo.Rows.Count > 0)
            {
                if (DT_UserInfo.Rows[0]["Username"].ToString() == username && DT_UserInfo.Rows[0]["Password"].ToString() == password)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        ///     获取指定用户权限
        /// </summary>
        /// <param name="username" type="string">
        ///     <para>
        ///         用户名
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回非负数则表示获取成功
        /// </returns>
        public int GetUserMession(String username)
        {
            DataTable DT_UserInfo = Globle.userService.GetUserByName(username);
            if (DT_UserInfo != null && DT_UserInfo.Rows.Count > 0)
            {
                return int.Parse(DT_UserInfo.Rows[0]["Mession"].ToString());
            }
            return -1;
        }

        /// <summary>
        ///     获得指定用户的Id
        /// </summary>
        /// <param name="username" type="string">
        ///     <para>
        ///         用户名
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回null , 则表示不存在该用户
        /// </returns>
        public String GetUserId(String username)
        {
            DataTable DT_UserInfo = Globle.userService.GetUserByName(username);
            if (DT_UserInfo != null && DT_UserInfo.Rows.Count > 0)
            {
                return DT_UserInfo.Rows[0]["UserId"].ToString();
            }
            return null;
        }

        /// <summary>
        ///     用过用户ID得到用户帐号
        /// </summary>
        /// <param name="UserId" type="string">
        ///     <para>
        ///         用户ID
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回用户ID
        /// </returns>
        public String GetUsernameById(String UserId)
        {
            DataTable DT_UserInfo = Globle.userService.GetUserByName(int.Parse(UserId));
            if (DT_UserInfo != null && DT_UserInfo.Rows.Count > 0)
            {
                return DT_UserInfo.Rows[0]["Username"].ToString();
            }
            return null;
        }

        /// <summary>
        ///     获取指定用户的所有信息字典
        /// </summary>
        /// <param name="username" type="string">
        ///     <para>
        ///         要查询的用户名
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回null,则表示没有该用户
        /// </returns>
        public Dictionary<String,String> GetFullUserInfo(String username)
        {
            DataTable DT_UserInfo = Globle.userService.GetUserByName(username);
            if (DT_UserInfo != null && DT_UserInfo.Rows.Count > 0)
            {
                Dictionary<String,String> DIC_USERINFO = new Dictionary<string, string>();
                int Offset = 0;
                foreach (DataColumn dc in DT_UserInfo.Columns)
                {
                    DIC_USERINFO.Add(dc.ColumnName.ToUpper(), DT_UserInfo.Rows[0][Offset].ToString());
                    Offset++;
                }
                return DIC_USERINFO;
            }
            return null;
        }

        /// <summary>
        ///     获取指定用户的所有信息字典
        /// </summary>
        /// <param name="username" type="string">
        ///     <para>
        ///         要查询的用户名
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回null,则表示没有该用户
        /// </returns>
        public Dictionary<String, String> GetFullUserInfo(int UserId)
        {
            DataTable DT_UserInfo = Globle.userService.GetUserByName(UserId);
            if (DT_UserInfo != null && DT_UserInfo.Rows.Count > 0)
            {
                Dictionary<String, String> DIC_USERINFO = new Dictionary<string, string>();
                int Offset = 0;
                foreach (DataColumn dc in DT_UserInfo.Columns)
                {
                    DIC_USERINFO.Add(dc.ColumnName.ToUpper(), DT_UserInfo.Rows[0][Offset].ToString());
                    Offset++;
                }
                return DIC_USERINFO;
            }
            return null;
        }

        /// <summary>
        ///     修改用户个性签名
        /// </summary>
        /// <param name="username" type="string">
        ///     <para>
        ///         用户名
        ///     </para>
        /// </param>
        /// <param name="NewProd" type="string">
        ///     <para>
        ///         新的个性签名
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回false , 则表示更新失败
        /// </returns>
        public bool ModifyUserProd(String username, String NewProd)
        {
            return Globle.userService.ModifyUserProd(username, NewProd);
        }

        /// <summary>
        ///     更新指定用户字段信息
        /// </summary>
        /// <param name="username" type="string">
        ///     <para>
        ///         用户名
        ///     </para>
        /// </param>
        /// <param name="DB_FILED" type="string">
        ///     <para>
        ///         数据库关联字段
        ///     </para>
        /// </param>
        /// <param name="NewValue" type="string">
        ///     <para>
        ///         新值
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回 false, 则表示更新失败
        /// </returns>
        public bool Update(String username, String DB_FILED, String NewValue)
        {
            return Globle.userService.Update(username, DB_FILED, NewValue);
        }
    }
}
